<?php
function __autoload($ten){
	$ten=str_replace("_","/",$ten);
	$ten=strtolower(str_replace("Model","Models",$ten));
	if(file_exists(BASE_PATH . "/$ten.php")){
		require ("$ten.php");
	}
}
function cutstring($str, $length){
	if(!$str || !$length){
		return $str;
	}
	if(mb_strlen($str) > $length){
		return mb_substr($str, 0, $length, 'UTF-8') . '...';
	}else{
		return $str;
	}
}
function loadview($ten,$data=""){
	require("views/$ten.php");
}
function redirect($url){
	ob_end_clean();
	header("location:$url");
	exit();
}
function redirectjs($str,$url){
	loadview("template/header2");
	echo "<script>alert('".$str."');location.href='".$url."'</script>";
	exit();
}
function filter($string)
{
$string1=$string;
$thay='*';
return preg_replace("/(location\.)|(alert)|(document\.)|(script)|(implementation)|(livescript)|(LAYER)|(mocha)|(select)|(union)|(concat)/i", $thay, $string1);
}
if (get_magic_quotes_gpc()) {
 function stripslashes_deep($value)
 	{
       $value = is_array($value) ?
        array_map('stripslashes_deep', $value) :
                    stripslashes(filter($value));
        return $value;
	}  
	  $_POST =stripslashes_deep($_POST);
	  $_GET =stripslashes_deep($_GET);
}
 function khongdau($str) {
	 
     $str = preg_replace("/(\/|!|>|<|^|,|;|#|%|'|:|\"|\?|=|\|\$|\*|–|&)/", '', $str);
	  $str = preg_replace("/(_|\.)/", '_', $str);
	 $str = preg_replace("/(\[|\])/", '', $str);    
     $str = str_replace('\'','',$str);
    $str = str_replace('+','-',$str);  
	 $str = str_replace('~','-',$str); 
	$str = preg_replace("(\()", '', $str);
	$str = preg_replace("(\))", '', $str); 	
   $str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
	$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
	$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
	$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
	$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
	$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
	$str = preg_replace("/(đ)/", 'd', $str);
	$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
	$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
	$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
	$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
	$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
	$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
	$str = preg_replace("/(Đ)/", 'D', $str);
	$str = preg_replace("/'/", '', $str); 
	$str = preg_replace("//", '', $str); 
	$str = preg_replace("/%/", '', $str); 
	$str=trim($str);
	$str = preg_replace("(%20| )", '-', $str);
	 $str = preg_replace('/-+/','-',$str); 
	 $str = preg_replace('/--/','-',$str); 
	return $str; 
}
function url() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
function echo2($str)
{
	 echo htmlspecialchars($str,ENT_QUOTES,'utf-8');
}
function encode($str)
{
	 return htmlspecialchars($str,ENT_QUOTES,'utf-8');
}
function get_ip()
{
     $ip = false;
     if(!empty($_SERVER['HTTP_CLIENT_IP']))
     {
          $ip = $_SERVER['HTTP_CLIENT_IP'];
     }
     if(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
     {
          $ips = explode(", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
          if($ip)
          {
              array_unshift($ips, $ip);
               $ip = false;
          }
          for($i = 0; $i < count($ips); $i++)
          {
               if(!preg_match("/^(10|172\.16|192\.168)\./i", $ips[$i]))
               {
                    if(version_compare(phpversion(), "5.0.0", ">="))
                    {
                         if(ip2long($ips[$i]) != false)
                         {
                              $ip = $ips[$i];
                              break;
                        }
                    }
                    else
                    {
                         if(ip2long($ips[$i]) != - 1)
                         {
                              $ip = $ips[$i];
                             break;
                         }
                    }
               }
          }
     }
     return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}  
function randomstring($length = 10) {
    return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}
function time_ago($date)
{
	if(empty($date)){
		return "";
	}
	$periods = array("giây", "phút", "giờ", "ngày", "tuần", "tháng", "năm", "10 năm");
	$lengths = array("60","60","24","7","4.35","12","10");
	$now = time();
	$unix_date = strtotime($date);
// check validity of date
	if(empty($unix_date)) {
	return $date;
	}
// is it future date or past date
	if($now > $unix_date) {
	$difference = $now - $unix_date;
	$tense = "trước";
	} else {
	$difference = $unix_date - $now;
	$tense = "sau";}
	for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
	$difference /= $lengths[$j];
	}
	$difference = round($difference);
	if($difference != 1) {
	$periods[$j].= "";
	}
	return "$difference $periods[$j] {$tense}";
}
function time_ago2($date){
  if(empty($date)) {
        return "No date provided";
    }
 
    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths         = array("60","60","24","7","4.35","12","10");
 
    $now             = time();
    $unix_date         = strtotime($date);
 
       // check validity of date
    if(empty($unix_date)) {    
        return "Bad date";
    }
 
    // is it future date or past date
    if($now > $unix_date) {    
        $difference     = $now - $unix_date;
        $tense         = "ago";
 
    } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";
    }
 
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }
 
    $difference = round($difference);
 
    if($difference != 1) {
        $periods[$j].= "s";
    }
 
    return "$difference $periods[$j] {$tense}";
}
function web_title($str,$where=''){
								
				if(isset($_GET['page'])){
					$str.= ' - Trang '.$_GET['page'];
				}
			
	return $str;
}
function web_des($str,$where=''){
							
				if(isset($_GET['page'])){
					$str.= ' Trang '.$_GET['page'];
				}
			
	return $str;
}
function web_keyword($str,$where=''){
							
				if(isset($_GET['page'])){
					$str.= ' Trang '.$_GET['page'];
				}
			
	return $str;
}
function url_ajax($op=''){
	
	$url=url();
	if($op=='category'){
		
	}
	if(!isset($_GET['page'])){
		$lw=substr($url,-1);
		if($lw!='/'){
		 $url.='/';
		}
	}
	
	
	if(isset($_GET['page']) && $_GET['page']==''){
		$lw=substr($url,-1);
		if($lw!='/'){
		 $url.='/';
		}
	}
	
	if(isset($_GET['page']) && $_GET['page']!=''){
		$lw=substr($url,-1);
		if($lw!='/'){
			 $data=strrpos($url,'/');
			 $url=substr($index,0,($data+1));
		}
		
	}
	
	return $url;
}
function mycurl($data,$url){
	//open connection
	$ch = curl_init();
	//set the url, number of POST vars, POST data
	curl_setopt($ch,CURLOPT_URL, $url);
	curl_setopt($ch,CURLOPT_POST, count($data));
	curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
	//execute post
	$result = curl_exec($ch);
	return $result;
	//close connection
	curl_close($ch);
}
function short_str($content,$num){ 
	if(strlen($content) < $num){
	return $content;
	}else{
	//$pos=mb_strpos($content, ' ', $num);
	return mb_substr($content,0,$num, 'UTF-8' ) . '...'; 
	}
}
function direct_url(){
		$url=url();
		if(isset($_GET['page']) && $_GET['page']==1){
			$le=strlen($url);
			
			if(substr($url,$le-1,1)==1){
			$str2=substr($url,0,$le-2);
			redirect($str2);
			exit();
			}
			
		}
}
function table_chuong($id){
$num=ceil($id/50000);
return "truyen_chuong_noidung".$num;	
}
function check_null($var){
	if(trim($var)){
		return $var;
		}else{return NULL;}
	}
	
function get_group($id){
	switch($id){
		case "1" : $str="Administrator"; break;
		case "2" : $str="Editor" ; break;
		case "3" :	$str="Uploader"; break;
		case "4" : $str="Member";break;
	}
	return $str;
}
 
function picasa_remote($url,$type='manga'){
 													 if($url!=''){
														 $url_remote="http://up.manga24h.com/upload_remote.php?link=$url&server=picasa&id=$type";
														 $data='';
														 $res= mycurl($data,$url_remote);
														
														 $res=json_decode($res,true);
														
															if($res['error']==false &&$res['url']!=''){
																
																return $res['url'];
															}else{
																//Doupload lan 2
																 sleep(2);
																 $url_remote="http://up.manga24h.com/tct_upload.php?link=$url&server=picasa&id=$type";
																 $res= mycurl($data,$url_remote);
																 				
																 $res=json_decode($res,true);
																if($res['error']==false&&$res['url']!=''){
																		return $res['url'];
																
																}else{
																	return false;
																}
															}
													 }else{ //$URL !=''
													 	return false;
													 }
 }
